﻿namespace HRM.CORE.CLASS
{
    using APP.HELPER.Data.Helper;
    using Microsoft.VisualBasic;
    using System; using Extra.ERP;
    using System.Data;
    using System.Windows.Forms; using Common;

    public class clsDocumentNumberOption
    {
        private int m_Length;
        private string m_Prefix;
        private string m_Suffix;

        public clsDocumentNumberOption(string Code, string PrefixDefault, string SuffixDefault)
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Application.StartupPath + @"\Layout\documentNumberOption.xml");
                this.m_Prefix = CheckOption(ds, Code, "Prefix");
                this.m_Length = int.Parse(CheckOption(ds, Code, "Length"));
                this.m_Suffix = CheckOption(ds, Code, "Suffix");
            }
            catch
            {
                this.m_Prefix = PrefixDefault;
                this.m_Length = 6;
                this.m_Suffix = SuffixDefault;
            }
        }

        public static string CheckOption(DataSet ds, string Code, string FieldName)
        {
            switch (Code)
            {
                case "CandidateCode":
                    return ds.Tables[0].Rows[0][FieldName].ToString();

                case "EmployeeCode":
                    return ds.Tables[0].Rows[1][FieldName].ToString();

                case "ContractCode":
                    return ds.Tables[0].Rows[2][FieldName].ToString();
            }
            return ds.Tables[0].Rows[0][FieldName].ToString();
        }

        public string GenCode(string tableName, string columnName)
        {
            object obj2;
            SqlHelper helper = new SqlHelper {
                CommandType = CommandType.Text
            };
            string expression = "";
            if ((this.m_Prefix != "") || (this.m_Suffix != ""))
            {
                obj2 = helper.ExecuteScalar("select max([" + columnName + "]) from [" + tableName + "] where [" + columnName + "] like N'" + this.m_Prefix + "%" + this.m_Suffix + "'");
            }
            else
            {
                obj2 = helper.ExecuteScalar("select max([" + columnName + "]) from [" + tableName + "] where [" + columnName + "] like N'0%' or [" + columnName + "] like N'1%' or [" + columnName + "] like N'2%' or [" + columnName + "] like N'3%' or [" + columnName + "] like N'4%' or [" + columnName + "] like N'5%' or [" + columnName + "] like N'6%' or [" + columnName + "] like N'7%' or [" + columnName + "] like N'8%' or [" + columnName + "] like N'9%'");
            }
            expression = (obj2 == null) ? "0" : obj2.ToString();
            if (this.m_Prefix.Length != 0)
            {
                expression = expression.Replace(this.m_Prefix, "").Trim();
            }
            if (this.m_Suffix.Length != 0)
            {
                expression = expression.Replace(this.m_Suffix, "").Trim();
            }
            int num = 0;
            if (Information.IsNumeric(expression))
            {
                num = (int) Conversion.Val(expression);
            }
            num++;
            string str2 = num.ToString();
            while (str2.Length < this.m_Length)
            {
                str2 = "0" + str2;
            }
            return (this.m_Prefix + str2 + this.m_Suffix);
        }

        public static void SaveOption(DataTable Table)
        {
            DataSet set = new DataSet();
            set.Tables.Add(Table);
            try
            {
                set.WriteXml(Application.StartupPath + @"\Layout\documentNumberOption.xml");
            }
            catch
            {
            }
        }

        public int Length
        {
            get
            {
                return this.m_Length;
            }
            set
            {
                this.m_Length = value;
            }
        }

        public string Prefix
        {
            get
            {
                return this.m_Prefix;
            }
            set
            {
                this.m_Prefix = value;
            }
        }

        public string Suffix
        {
            get
            {
                return this.m_Suffix;
            }
            set
            {
                this.m_Suffix = value;
            }
        }
    }
}

